<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="数据类型名称" prop="typeName">
        <el-input v-model="queryParams.typeName" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="文件名" prop="fileNamePrefix">
        <el-input v-model="queryParams.fileNamePrefix" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <el-table-column prop="typeName" label="数据类型名称" align="center"></el-table-column>
      <el-table-column prop="storeDir" label="文件路径" align="center"></el-table-column>
      <el-table-column label="文件名" align="center" prop="fileName"></el-table-column>
      <el-table-column label="文件大小" align="center" prop="maxFileSize"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改部门对话框 -->
    <Edit v-if="dialogVisible" :dialogTitle="dialogTitle" :dialogOpen="dialogOpen" :dialogData="dialogData"
      @closeDialog="closeDialog" />
    <!--详情-->
    <Detail v-if="dialogVisible" :dialogTitle="dialogTitleDetail" :dialogOpen="dialogOpenDetail"
      @closeDialog="closeDialog" />
  </div>
</template>

<script>
import {
  listDataTypes,
  changeTypeStatus,
  removeDataType,
} from "@/api/statistical";
import Edit from "./components/edit.vue";
import Detail from "./components/detail.vue";

export default {
  name: "Datasource",
  components: { Edit, Detail },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      dataList: [],
      // 重新渲染表格状态
      refreshTable: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeName: undefined,
        fileNamePrefix: undefined,
      },
      sourceCodeDatas: [],
      //弹窗参数
      dialogVisible: false,
      dialogTitle: "",
      dialogOpen: false,
      dialogData: {},
      dialogOpenDetail: false,
      dialogTitleDetail: ''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listDataTypes(this.queryParams).then((response) => {
        console.log(224, response.rows);
        response.rows.forEach((item) => {
          item.fileName = item.fileNamePrefix + "." + item.fileNameSuffix;
          // item.fileSize = this.convertFileSize(item.maxFileSize);
          // let units = ["KB", "MB", "GB", "TB"];
          // if (item.maxFileSize >= 1024) {
          //   for (let i = 0; item.maxFileSize >= 1024 && i < 4; i++) {
          //     item.maxFileSize = parseFloat(
          //       (parseInt(item.maxFileSize) / 1024).toFixed(2)
          //     )
          //     item.fileSize = item.maxFileSize + units[i+1];
          //   }
          // }else {
          //   item.fileSize = item.maxFileSize + "KB";
          // }
        });
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态修改
    handleStatusChange(row) {
      console.log(227, row);
      let text = row.status == 0 ? "运行" : "停止";
      this.$modal
        .confirm("确认要" + text + "该项吗？")
        .then(function () {
          return changeTypeStatus(row.id);
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
          this.getList()
        })
        .catch(function () {
          // row.status = row.status == 0 ? 1 : 0;
        });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //关闭库相关
    closeDialog() {
      this.dialogVisible = false;
      this.dialogOpen = false;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      delete this.queryParams.status
      this.handleQuery();
    },
    /** 详情按钮操作 */
    handleDetail() {
      this.dialogOpenDetail = true;
      this.dialogTitleDetail = "数据记录详情";
      this.dialogVisible = true;
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(218, row);
      this.dialogOpen = true;
      this.dialogTitle = "编辑数据记录";
      this.dialogVisible = true;
      this.dialogData = row;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("确定要删除该数据项？")
        .then(function () {
          return removeDataType([ids]);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
